package service

import (
	"context"
	"zk2/you-ku-dev/video-server/basic/config"
	"zk2/you-ku-dev/video-server/basic/proto"
	"zk2/you-ku-dev/video-server/handler/model"
)

type Server struct {
	proto.UnimplementedVideoServer
}

func (s Server) CreateVideo(_ context.Context, in *proto.CreateVideoReq) (*proto.CreateVideoResp, error) {
	//TODO implement me

	var video model.Video

	if video.Title != "" {
		return &proto.CreateVideoResp{
			Code: 0,
			Msg:  "参数不能为空",
		}, nil
	}
	video = model.Video{
		Title:    in.Title,
		Type:     in.Type,
		VideoUrl: in.VideoUrl,
	}
	err := model.CreateVideo(&video)
	if err != nil {
		return &proto.CreateVideoResp{
			Code: 0,
			Msg:  "添加失败",
		}, nil
	}
	return &proto.CreateVideoResp{
		Code: 200,
		Msg:  "添加成功",
		Data: int64(video.ID),
	}, nil
}
func (s Server) CreateTVSeries(_ context.Context, in *proto.CreateTVSeriesReq) (*proto.CreateTVSeriesResp, error) {

	var TvSeries model.TVSeries
	tx := config.DB.Begin()
	TvSeries = model.TVSeries{
		Name:    in.Name,
		Content: in.Content,
		TvTime:  in.TvTime,
	}
	err := model.CreateTv(&TvSeries)
	if err != nil {
		return &proto.CreateTVSeriesResp{
			Code: 0,
			Msg:  "添加失败",
		}, nil
		tx.Rollback()
	} else {

		var Series model.Series
		Series = model.Series{
			Tid:   int(in.Tid),
			Title: in.Title,
			Free:  int(in.Free),
			STime: in.STime,
		}
		err = model.CreateSeries(&Series)
		if err != nil {
			return &proto.CreateTVSeriesResp{
				Code: 0,
				Msg:  "添加失败",
			}, nil
		}
		tx.Rollback()

	}
	return &proto.CreateTVSeriesResp{
		Code: 200,
		Msg:  "添加成功",
		Data: int64(TvSeries.ID),
	}, nil

}
func (s Server) CreatePram(_ context.Context, in *proto.CreatePramReq) (*proto.CreatePramResp, error) {

	var pram model.Pram
	pram = model.Pram{
		Name:    in.Name,
		Type:    in.Type,
		Address: in.Address,
		SID:     int(in.SID),
	}
	err := model.CreatePram(&pram)
	if err != nil {
		return &proto.CreatePramResp{
			Code: 0,
			Msg:  "添加失败",
		}, nil
	}
	return &proto.CreatePramResp{
		Code: 200,
		Msg:  "添加成功",
		Data: int64(pram.ID),
	}, nil
}
func (s Server) CreateVideoStatus(_ context.Context, in *proto.CreateVideoStatusReq) (*proto.CreateVideoStatusResp, error) {

	var VideoStatus model.VideoStatus
	VideoStatus = model.VideoStatus{

		VideoId: int(in.VideoId),
		Status:  in.Status,
		Text:    in.Text,
	}
	err := model.CreateStatus(&VideoStatus)
	if err != nil {
		return &proto.CreateVideoStatusResp{
			Code: 0,
			Msg:  "添加失败",
		}, nil
	}
	return &proto.CreateVideoStatusResp{
		Code: 200,
		Msg:  "添加成功",
		Data: int64(VideoStatus.ID),
	}, nil
}
func (s Server) CreateUser(_ context.Context, in *proto.CreateUserReq) (*proto.CreateUsersResp, error) {

	var user model.User
	user = model.User{
		Username: in.Username,
		Free:     int(in.Free),
	}
	err := model.CreateUser(&user)
	if err != nil {
		return &proto.CreateUsersResp{
			Code: 0,
			Msg:  "注册失败",
		}, nil
	}
	return &proto.CreateUsersResp{
		Code: 200,
		Msg:  "注册成功",
		Data: int64(user.ID),
	}, nil
}
func (s Server) FindUser(_ context.Context, in *proto.FindUserReq) (*proto.FindUserResp, error) {

	var user model.User
	err := model.FindUser(&user, int(in.Free))
	if err != nil {
		return &proto.FindUserResp{
			Code: 0,
			Msg:  "无vip权限",
		}, nil
	}
	return &proto.FindUserResp{
		Code: 200,
		Msg:  "vip已过期",
		Data: int64(user.ID),
	}, nil
}
